package com.lynu.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lynu.controller.UserController;
import com.lynu.pojo.Headline;
import com.lynu.pojo.PortalOv;
import com.lynu.service.HeadlineService;
import com.lynu.mapper.HeadlineMapper;
import com.lynu.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.crypto.spec.PSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 31536
* @description 针对表【news_headline】的数据库操作Service实现
* @createDate 2024-04-15 21:33:13
*/
@Service
public class HeadlineServiceImpl extends ServiceImpl<HeadlineMapper, Headline>
    implements HeadlineService{
    @Autowired
    private HeadlineMapper headlineMapper;
    @Override
    public Result findNewsPages(PortalOv portalOv) {
        String keyWords=portalOv.getKeyWords();
        Integer type=portalOv.getType();
        Integer pageNum=portalOv.getPageNum();
        Integer pageSize=portalOv.getPageSize();
        Integer offset=(pageNum-1)*pageSize;
        Map result=new HashMap<>();
        Map inner=new HashMap();
        List<Headline> lists=headlineMapper.findNewsPages(keyWords,type,offset,pageSize);
        Integer totalSize= lists.size();
        System.out.println(lists);
        Integer totalPage=totalSize/pageSize;
        if(totalSize%pageSize!=0){
            totalPage++;
        }

        inner.put("pageData",lists);
        result.put("pageInfo",inner);
        result.put("pageNum",pageNum);
        result.put("pageSize",pageSize);
        result.put("totalPage",totalPage);
        result.put("totalSize",totalSize);
        return Result.ok(result);
    }

    @Override
    public Result showHeadLineDetail(Integer hid) {
        Map headlineMap=headlineMapper.selectMapByHid(hid);
        System.out.println(headlineMap);
        headlineMapper.updateViews(hid);
        Map result=new HashMap();
        result.put("headline",headlineMap);
        return Result.ok(result);
    }

    @Override
    public Result publish(Headline headline) {
        Integer publisher=headline.getPublisher();
        String title= headline.getTitle();
        String article=headline.getArticle();
        Integer type=headline.getType();
        headlineMapper.publish(title,article,type,publisher);
        return  Result.ok(null);
    }

    @Override
    public Result findHeadlineByHid(Integer hid) {
        Headline headline=headlineMapper.selectById(hid);
        System.out.println(headline);
        Map map=new HashMap();
        map.put("headline",headline);
        return Result.ok(map);
    }

    @Override
    public Result update(Headline headline) {
        Headline updateHeadline=new Headline();
        updateHeadline.setTitle(headline.getTitle());
        updateHeadline.setArticle(headline.getArticle());
        updateHeadline.setType(headline.getType());
        UpdateWrapper<Headline> wrapper=new UpdateWrapper<>();
        wrapper.eq("hid",headline.getHid());
        headlineMapper.update(updateHeadline,wrapper);
        return Result.ok(null);
    }

    @Override
    public Result removeByHid(Integer hid) {
        headlineMapper.removeByHid(hid);
        return Result.ok(null);
    }
}




